package cst.neu.yuejian_31.model;

import java.util.List;

import cst.neu.yuejian_31.bean.MasterBean;
import cst.neu.yuejian_31.iface.MasterIface;
import cst.neu.yuejian_31.iface.MasterListerner;
import cst.neu.yuejian_31.Common.Constant;
import cst.neu.yuejian_31.service.MasterService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Alienware on 2017/10/19.
 */

public class MasterModel implements MasterIface {
    private Retrofit retrofit;

    //    private String BASEURL="http://10.0.2.2/neu16_yuejian/api/Club/";
    public MasterModel() {
        retrofit = new Retrofit.Builder()
                .baseUrl(Constant.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    @Override
    public void getmasterList(final MasterListerner MasterListener) {
        MasterService MasterService = retrofit.create(MasterService.class);
        Call<List<MasterBean>> call
                = MasterService.getmasterlist();
        call.enqueue(new Callback<List<MasterBean>>() {
            @Override
            public void onResponse(Call<List<MasterBean>> call, Response<List<MasterBean>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    MasterListener.onResponse(response.body());
                } else {
                    MasterListener.onFail("FAIL");

                }
            }

            @Override
            public void onFailure(Call<List<MasterBean>> call, Throwable t) {

                MasterListener.onFail(t.toString());
            }
        });
    }
}